package com.star.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.star.domain.UserLogin;
import com.star.dto.ResponseData;
import com.star.dto.Result;
import com.star.mapper.UserLoginMapper;
import com.star.service.UserLoginService;
import com.star.util.Emai;
import com.star.util.JavaWebToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

/**
 * <p>
 * 用户登录表
 * </p>
 *
 * @author Star
 * @since 2021-04-21
 */
@Controller
@RequestMapping("/userLogin")
public class UserLoginController {
    @Autowired
    private UserLoginService userLoginService;

    /**
     * 登录用户注册
     *
     * @param userLogin 注册登录信息对象
     * @param headImg   图片
     * @return 对象
     */
    @ResponseBody
    @RequestMapping("/registerUserLogin")
    private Result<UserLogin> registerUserLogin(UserLogin userLogin, MultipartFile headImg,String email) {
        Result<UserLogin> userLoginResult = null;
        try {
            userLoginResult = userLoginService.userLoginRegister(userLogin, headImg,email);
        } catch (Exception e) {
            return new Result<>(false, 400, "注册用户失败");
        }
        return userLoginResult;
    }

    /**
     * 判断用户名密码是否一样
     * 用户登录
     *
     * @param userLogin 对象
     * @return
     */
    @RequestMapping("/login")
    private @ResponseBody
    ResponseData login(UserLogin userLogin) {
        UserLogin login = userLoginService.LoginByNameAndPwd(userLogin);
        ResponseData responseData = ResponseData.ok();
        if (login != null) {
            // 生成token
            String token = JavaWebToken.generToken(String.valueOf(userLogin.getLogId()), "Kuroko-Security-Basic", userLogin.getLogAccount());
            // 向浏览器返回token，客户端收到此token后存入cookie中，或者h5的本地存储
            responseData.putDataValue("token", token);
            // 用户信息 存储
            responseData.putDataValue("userLogin", login);
        } else {
            // 用户名或者密码错误
            responseData = ResponseData.customerError();
        }
        return responseData;
    }

    /**
     * 判断是不是唯一的登录名
     *
     * @param logAccount 登录名
     * @return 对象
     */
    @ResponseBody
    @RequestMapping("/byLoginName")
    private Boolean judgeUserLoginName(String logAccount) {
        return userLoginService.ByUserLoginName(logAccount);
    }

    /**
     * 更新密码
     * @return
     */
    @RequestMapping("/updatePassword")
    private @ResponseBody
    ResponseData updatePassword() {
        ResponseData responseData = ResponseData.ok();
        return null;
    }

    /**
     * 验证码
     * @param email 发送邮箱地址
     * @param userName 用户名
     * @return 对象
     * @throws Exception
     */
    @RequestMapping("/byCode")
    private @ResponseBody ResponseData byCode(String email,String userName) throws Exception {
        ResponseData responseData = null;
        /**
         * 发送返回获取 获取验证码
         */
        String codeTo;
        try {
            codeTo = Emai.email(email, "星速外卖验证码",userName,"注册账户");
            //验证码存储
            responseData = ResponseData.ok();
            responseData.putDataValue("codeTo",codeTo);
        }catch (Exception e){
            responseData = ResponseData.customerError();
        }
        return responseData ;
    }
}

